﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class TacGiaDAO
    {
        #region 1. Them
        public static bool ThemTacGia(TacGiaDTO tacgia)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTG", tacgia.MaTG));
                sqlParams.Add(new SqlParameter("@Ten", tacgia.Ten));                
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertTACGIum", sqlParams);               
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 1. Sua
        public static bool SuaTacGia(TacGiaDTO tacgia)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTG", tacgia.MaTG));
                sqlParams.Add(new SqlParameter("@Ten", tacgia.Ten));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateTACGIum", sqlParams);

                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 1. Them
        public static bool XoaTacGia(int matg)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTG", matg));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteTACGIum", sqlParams);

                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Tim kiem
        public static TacGiaDTO TimTacGia(int matg){
            TacGiaDTO result = new TacGiaDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTG", matg));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectTACGIum", sqlParams);
                DataRow dr = dt.Rows[0];

                result.MaTG = int.Parse(dr["MaTG"].ToString());
                result.Ten = dr["Ten"].ToString();

            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static DataTable TimTatCaTacGia(){
            DataTable   dt = new DataTable();
            try
            {
                 dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectTACGIAsAll");
               
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return dt;
        }
        public static bool CheckTacGiaByID(int matg){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTG", matg));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectTACGIum", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
    }
}
